package com.unohacha.qqj.fzdr.personal.view.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.bean.WuliuDetail;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.util.ToastUtils;
import com.unohacha.qqj.fzdr.view.permissions.RxPermissions;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class WuliuDetailActivity extends BaseActivity {
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.tv_wuliu_name)
    TextView tvWuliuName;
    @BindView(R.id.tv_people_name)
    TextView tvPeopleName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_company_site)
    TextView tvCompanySite;
    @BindView(R.id.tv_detail_site)
    TextView tvDetailSite;
    @BindView(R.id.tv_site_start)
    TextView tvSiteStart;
    @BindView(R.id.tv_site_middle)
    TextView tvSiteMiddle;
    @BindView(R.id.tv_site_end)
    TextView tvSiteEnd;
    private String id;//id

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wuliu_detail;
    }

    @Override
    protected void initView() {
        commonRightImage.setVisibility(View.INVISIBLE);
        commonTitle.setText("物流详情");
        id = this.getIntent().getStringExtra("id");
        getMessage();
    }

    private void getMessage() {
        RetrofitManager.getInstance().WuliuDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Action1<HttpResult<WuliuDetail>>() {
                               @Override
                               public void call(HttpResult<WuliuDetail> wuliuDetailHttpResult) {
                                   if (wuliuDetailHttpResult.getCode() == 1) {
                                       RefreshUI(wuliuDetailHttpResult.getData());
                                   } else {
                                       ToastUtils.showT("获取数据失败");
                                   }
                               }
                           },
                        new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {

                            }
                        });
    }

    private void RefreshUI(WuliuDetail date) {
        Glide.with(this)
                .load(date.getAttachment_id())
                .override(100, 75)
                .into(image);
        tvWuliuName.setText(date.getName());
        tvPeopleName.setText("联系人：" + date.getContact());
        tvPhone.setText(date.getTelephone());
        tvCompanySite.setText(date.getCompany_add());
        tvDetailSite.setText(date.getCompany_add());
        tvSiteStart.setText(date.getRoute_start());
        tvSiteMiddle.setText(date.getRoute_channel());
        tvSiteEnd.setText(date.getRoute_end());
    }

    @OnClick({R.id.common_back, R.id.tv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                finish();
                break;
            case R.id.tv_phone:
                RxPermissions rxPermissions = new RxPermissions(WuliuDetailActivity.this);
                rxPermissions.request(Manifest.permission.CALL_PHONE).subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            if (null != tvPhone.getText()) {
                                Intent intent = new Intent(Intent.ACTION_CALL);
                                Uri data = Uri.parse("tel:" + tvPhone.getText());
                                intent.setData(data);
                                if (ActivityCompat.checkSelfPermission(WuliuDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                    // TODO: Consider calling
                                    //    ActivityCompat#requestPermissions
                                    // here to request the missing permissions, and then overriding
                                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                    //                                          int[] grantResults)
                                    // to handle the case where the user grants the permission. See the documentation
                                    // for ActivityCompat#requestPermissions for more details.
                                    return;
                                }
                                startActivity(intent);
                            }
                        } else {
                            ToastUtils.showT("照相机权限未获得");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
                break;
        }
    }
}
